<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use GatewayWorker\BusinessWorker;
use GatewayWorker\Gateway;
use GatewayWorker\Register;
use Workerman\Worker;



class Run
{
    // public function __construct()
    // {
    //     require_once dirname(dirname(dirname( __DIR__))) . '/vendor/workerman/workerman/Autoloader.php';

    //     // 注意：这里与上个例子不同，使用的是websocket协议
    //     $ws_worker = new Worker("websocket://0.0.0.0:2000");

    //     // 启动4个进程对外提供服务
    //     $ws_worker->count = 4;

    //     // 当收到客户端发来的数据后返回hello $data给客户端
    //     $ws_worker->onMessage = function($connection, $message)
    //     {
    //         $message_data = json_decode($message, true);

    //         $new_message = array(
    //             'from_client_id'        => $message_data['from_client_id'],
    //             'from_client_name'      => $message_data['from_client_name'],
    //             'from_client_avatarurl' => $message_data['from_client_avatarurl'],
    //             'to_client_id'          => $message_data['to_client_id'],
    //             'to_client_name'        => $message_data['to_client_name'],
    //             'to_client_avatarurl'   => $message_data['to_client_avatarurl'],
    //             'content'               => nl2br(htmlspecialchars($message_data['content'])),
    //             'cdate'                 => date('Y-m-d H:i:s'),
    //             'ctime'                 => time()
    //         );

    //         db('chat')->insertGetId($new_message);
    //         $connection->send($message);
    //     };

    //     // 运行worker
    //     Worker::runAll();
    // }



    public function __construct()
    {
        //初始化各个GatewayWorker
        //初始化register
        new Register('text://0.0.0.0:1236');

        //初始化 bussinessWorker 进程
        $worker                  = new BusinessWorker();
        $worker->name            = 'YourAppBusinessWorker';
        $worker->count           = 100;
        $worker->registerAddress = '127.0.0.1:1236';
        //设置处理业务的类,此处制定Events的命名空间
        $worker->eventHandler = '\app\index\controller\Events';
        // 初始化 gateway 进程
        $gateway                  = new Gateway("websocket://0.0.0.0:7272");
        $gateway->name            = 'YourAppGateway';
        $gateway->count           = 4;
        $gateway->lanIp           = '127.0.0.1';
        $gateway->startPort       = 2900;
        $gateway->registerAddress = '127.0.0.1:1236';

        $gateway->pingInterval = 55;

        $gateway->pingNotResponseLimit = 1;

        $gateway->pingData = '';
        
        // 心跳间隔 
        // $gateway->pingInterval = 10; //s  
//         // 心跳数据  
        // $gateway->pingData = '{"type":"ping"}'; 
        // $gateway->pingNotResponseLimit = 1; // 多少此不回应 端口消息； 

//        $gateway->onConnect = function($connection)
//        {
//            $connection->onWebSocketConnect = function($connection , $http_header)
//            {
//                if (isset($_GET['user_id'])) {
//                    $_SESSION['user_id'] = $_GET['user_id'];
//                }
//                var_dump('我进入了服务器');
//                var_dump($_GET);
//            };
//
//        };

        Worker::runAll();
    }



}
